home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / VD08SMP.ZIP / usr / samples / Textview.prj / Controller.m < prev    next >
Encoding:
Text File  |  1996-02-13  |  4.6 KB  |  158 lines

  1. /* -------------------------------------------------------------------
  2.  
  3.     Project: 
  4.  
  5.     Objective-C source file for the class Controller
  6.  
  7.     This file was automatically generated by Project Editor
  8.     COPYRIGHT (C), 1995, Thomas Baier
  9.  
  10.     Unregistered version
  11.  
  12.     COPYRIGHT (C), 1996
  13.     ALL RIGHTS RESERVED.
  14.  
  15.     Date:                           Rev:
  16.     Sat Jan 06 14:33:21 1996
  17.  
  18.  
  19. ------------------------------------------------------------------- */
  20.  
  21. /* ------------------------ Include files ------------------------- */
  22. #include <io.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25.  
  26. /* ------------------------  Classes used  ------------------------ */
  27.  
  28.  
  29. /* -------------------------  Externals  -------------------------- */
  30.  
  31.  
  32. /* --------------------------  Defines  --------------------------- */
  33.  
  34.  
  35. /* ----------------------  Class variables  ----------------------- */
  36.  
  37.  
  38. /*====================================================================
  39.                      Implementation of class Controller
  40. ====================================================================*/
  41. #include "Controller.h"
  42.  
  43. @implementation Controller : Object
  44. {
  45.   id      mainWindow;
  46.   id      mleWindow;
  47. }
  48.  
  49. /*====================================================================
  50.                               Initialize
  51. ====================================================================*/
  52.  
  53.  
  54. /*====================================================================
  55.                                  Free
  56. ====================================================================*/
  57.  
  58.  
  59. /*====================================================================
  60.                Methods for access to Instance Variables
  61. ====================================================================*/
  62.  
  63.  
  64. /*====================================================================
  65.                             Public methods
  66. ====================================================================*/
  67.  
  68. /*--------------------------------------------------------------------
  69. |-loadFile: sender|
  70.  
  71. Return self.
  72. --------------------------------------------------------------------*/
  73. -loadFile: sender
  74. {
  75.   FileDlg *fileDlg = [[FileDlg alloc] initForOpen: "Open File..."
  76.               withFilter: "*.*"];
  77.   if (([fileDlg runModalFor: mainWindow]) && ([fileDlg result] == DID_OK))
  78.     [self readAndDisplayFile: [fileDlg fileName]];
  79.  
  80.   [fileDlg free];
  81.  
  82.   return self;
  83. }
  84.  
  85.  
  86. /*====================================================================
  87.                            Private methods
  88. ====================================================================*/
  89. /*--------------------------------------------------------------------
  90. |-readAndDisplayFile: (char *) aFileName| 
  91.  
  92. Return self.
  93.                                                           /1996-Jan-06
  94. --------------------------------------------------------------------*/
  95. -readAndDisplayFile: (char *) fileName
  96. {
  97.   char        *title;
  98.   FILE        *inputFile;
  99.   struct stat  statbuffer;
  100.   char        *contents;
  101.  
  102.   if (stat (fileName,&statbuffer) < 0) { /* check file */
  103.  
  104.     [mleWindow setText: ""];
  105.     [mainWindow setTitle: "Textview: no file loaded"];
  106.  
  107.     return nil;
  108.   }
  109.  
  110.   /*
  111.    * open file and read contents to buffer
  112.    */
  113.   inputFile = fopen (fileName,"r"); /* open text file read-only */
  114.       
  115.   contents = (char *) malloc (statbuffer.st_size + 1); /* allocate buffer */
  116.   fread (contents,statbuffer.st_size,1,inputFile); /* read contents of file */
  117.       
  118.   /*
  119.    * calculate title of window and set it
  120.    */
  121.   title = (char *) malloc (11 + /* this is the length of "Textview: " + 
  122.                    NULL */
  123.                strlen (fileName)); /* allocate buffer 
  124.                          for title */
  125.   sprintf (title,"Textview: %s",fileName); /* fill title buffer */
  126.   [mleWindow setText: contents]; /* display contents of file */
  127.   [mainWindow setTitle: title];
  128.  
  129.   free (title);
  130.   free (contents);
  131.  
  132.   fclose (inputFile); /* close file */
  133.   
  134.   return self;
  135. }
  136.  
  137.  
  138. /*====================================================================
  139.                           Archiving methods
  140. ====================================================================*/
  141. /*--------------------------------------------------------------------
  142. |-awakeFromInterfaceFile| 
  143.  
  144. Return self.
  145.                                                           /1996-Jan-06
  146. --------------------------------------------------------------------*/
  147. -awakeFromInterfaceFile
  148. {
  149.   [mainWindow bindCommand: 2001 withObject: self
  150.                  selector: @selector (loadFile:)];
  151.   [mainWindow bindCommand: 2002 withObject: mainWindow
  152.                  selector: @selector (performClose:)];
  153.  
  154.   return self;
  155. }
  156.  
  157. @end
  158.